<template>
  <div style="margin-top: 20px">六、计划任务书签约各方</div>
  <el-form
    label-width="auto"
    label-position="top"
    class="demo-ruleForm"
    style="margin-top: 20px"
  >
    <el-form-item label="（一）任务下达方：">
      <el-select
        v-model="ruleForm.taskIssuerId"
        placeholder="请选择"
        style="width: 300px"
      >
        <el-option
          v-for="item in depts"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="主管部门：">
      <el-select
        v-model="ruleForm.supervisorDepId"
        placeholder="请选择"
        style="width: 300px"
      >
        <el-option
          v-for="item in depts"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="二）任务承担方：：">
      <el-select
        v-model="ruleForm.taskBearersId"
        placeholder="请选择"
        style="width: 300px"
      >
        <el-option
          v-for="item in depts"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="承担单位：">
      <el-select
        v-model="ruleForm.bearerDepId"
        placeholder="请选择"
        style="width: 300px"
      >
        <el-option
          v-for="item in depts"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script setup>
  import { getDeptPage } from '@/api/system/dept/index'

  const $baseMessage = inject('$baseMessage')

  const depts = ref([])

  const ruleForm = reactive({
    taskIssuerId: '',
    supervisorDepId: '',
    taskBearersId: '',
    bearerDepId: '',
  })

  const getDepts = () => {
    getDeptPage().then((res) => {
      if (res.code == 0 || res.code == 200) {
        depts.value = res.data || []
      }
    })
  }

  const getFormData = () => {
    for (const key in ruleForm) {
      if (!ruleForm[key]) {
        $baseMessage('请选择计划任务书签约各方', 'error')
        return false
      }
    }

    return {
      ...ruleForm,
      taskIssuerName:
        depts.value.find((key) => key.id == ruleForm.taskIssuerId)?.name || '',
      supervisorDepName:
        depts.value.find((key) => key.id == ruleForm.supervisorDepId)?.name ||
        '',
      taskBearersName:
        depts.value.find((key) => key.id == ruleForm.taskBearersId)?.name || '',
      bearerDepName:
        depts.value.find((key) => key.id == ruleForm.bearerDepId)?.name || '',
    }
  }

  const setFormData = ({ projectPlanTaskInfo }) => {
    for (const key in ruleForm) {
      ruleForm[key] = projectPlanTaskInfo[key]
    }
  }

  getDepts()

  defineExpose({ getFormData, setFormData })
</script>

<style lang="scss" scoped></style>
